package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.show_bean.CultivateBean;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.example.minato.minastore.utils.AppUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XinEr on 2019/8/11.
 * Describe:
 */
public class Cultivate_Two_RecvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<CultivateBean.CoursesRecommendBean> recommendBeans;


    private int TYPE_TITLE = 1;
    private int TYPE_ITEM = 2;

    public Cultivate_Two_RecvAdapter(Context mContext, ArrayList<CultivateBean.CoursesRecommendBean> superList) {
        this.mContext = mContext;
        this.recommendBeans = superList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder = null;
        if (i == TYPE_TITLE) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.title_cultivate, viewGroup, false);
            holder = new TitleHolder(inflate);
        } else {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_cultivate, viewGroup, false);
            holder = new ItemHolder(inflate);
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TITLE;
        }
        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int itemViewType = getItemViewType(i);
        if (itemViewType == TYPE_TITLE) {
            TitleHolder titleHolder = (TitleHolder) viewHolder;
            titleHolder.tv_title.setText("猜你需要");
        } else {
            ItemHolder itemHolder = (ItemHolder) viewHolder;
            CultivateBean.CoursesRecommendBean bean = recommendBeans.get(i - 1);
            itemHolder.tv_author.setText("课时：" + bean.getOrgAuthId());
            itemHolder.tv_price.setText("¥" + bean.getPrice());
            Logger.i("------------------------" + AppUtils.COURSE_IMAGE_URL + bean.getCover());
//            if (bean.getType() == 2){//线下
//                Glide.with(mContext).load(AppUtils.COURSE_IMAGE_URL + bean.getCover()).placeholder(R.mipmap.offline_default).into(itemHolder.cultivate_Item1_Img);
//            }else{
//                Glide.with(mContext).load(AppUtils.COURSE_IMAGE_URL + bean.getCover()).placeholder(R.mipmap.online_default).into(itemHolder.cultivate_Item1_Img);
//            }
            if (bean.getType() == 2){//线下
//                Glide.with(mContext).load(AppUtils.COURSE_IMAGE_URL + bean.getCover()).placeholder(R.mipmap.offline_default).into(itemHolder.cultivate_Item1_Img);
                GlideUtils.loaderHanldeRoundImage(mContext, AppUtils.COURSE_IMAGE_URL + bean.getCover(), itemHolder.cultivate_Item1_Img,R.mipmap.online_default, DensityUtils.dp2px(mContext,8));

            }else{
//                Glide.with(mContext).load(AppUtils.COURSE_IMAGE_URL + bean.getCover()).placeholder(R.mipmap.online_default).into(itemHolder.cultivate_Item1_Img);
                GlideUtils.loaderHanldeRoundImage(mContext, AppUtils.COURSE_IMAGE_URL + bean.getCover(), itemHolder.cultivate_Item1_Img,R.mipmap.online_default, DensityUtils.dp2px(mContext,8));

            }

            itemHolder.tv_chubanshe.setText("开课地点：" + bean.getClassPlace());
            itemHolder.tv_title.setText(bean.getName());
            itemHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(bean.getType()+"");
                    sb.append(bean.getId()+"");
                    Log.e("cjx",sb.toString());
                    mContext.startActivity(CourseDetailActivity.getIntent(mContext,sb.toString(),bean.getCover(),""));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return recommendBeans.size() + 1;
    }

    class TitleHolder extends RecyclerView.ViewHolder {
        TextView tv_title;

        public TitleHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }


    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cultivate_Item1_Img)
        ImageView cultivate_Item1_Img;
        @BindView(R.id.cultivate_Item1_Author)
        TextView tv_author;
        @BindView(R.id.cultivate_Item1_ChuBanShe)
        TextView tv_chubanshe;
        @BindView(R.id.cultivate_Item1_Price)
        TextView tv_price;
        @BindView(R.id.cultivate_Item1_Title)
        TextView tv_title;
        @BindView(R.id.cultivate_Item1_LinearLayout)
        LinearLayout linearLayout;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
